<?php

class MemberController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        if(!isset($this->sess)){
            $param = array(
            'action' => 'index',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->_helper->layout->setLayout('member');

        $this->member = new Application_Model_Member();
        $newMembers = $this->member->getNewMembers(20);
        $this->view->newMembers = $newMembers;

        $rewardMember = $this->member->getRewardMembers(20);
        $this->view->rewardMember = $rewardMember;

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

       

    }

    public function indexAction()
    {
        $memberId = $this->sess->member_id;
        $memberModel = new Application_Model_Member();
        $memberData = $memberModel->getMemberDetail($memberId);

        $modelFunc = new Application_Model_Functions();
        $country = $modelFunc->getCountry($memberData['country']) ;
        $this->view->data = $memberData;
        $this->view->country = $country;
    }

    public function gensnAction(){
        $sn = new Application_Model_Sn();
        $snpin = $sn->genSN();
        echo $snpin;
    }


    public function changepassAction(){
        //

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            $memberId = $this->sess->member_id;
            $oldPass = $formData['old_pass'];
            $newPass = $formData['new_pass'];
            $newPass2 = $formData['new_pass2'];

            $errorMsg = "";
            
            if ($newPass != $newPass2){
                $errorMsg .= "* Password Baru dan Konfirmasi Password Baru tidak cocok.<br>";
            }

            if ($errorMsg == ""){

                $modelMember = new Application_Model_Member();
                $data = $modelMember->changePass($memberId, $oldPass, $newPass);

                $code = $data['code'];
                $msg = $data['msg'];

                if($code == 0){
                        $this->view->code = $code;
                        $this->view->msg = $msg;
                }else{
                    $this->view->code = '1';
                    $this->view->msg = "ERROR ".$msg;
                }
            }

        }else{
            $modelMember = new Application_Model_Member();
            $memberTypeList = $modelMember->getMemberTypeList();
            $contactTypeList = $modelMember->getContactTypeList();

            $this->view->sess = (array) $this->sess;
            $this->view->memberTypeList = $memberTypeList;

            $this->view->code = isset($this->param['code']) ? $this->param['code'] : "";
            $this->view->msg = isset($this->param['msg']) ? $this->param['msg'] : "";

            $countryList = new Application_Model_Functions();
            $this->view->countryList = $countryList->countryList();
        }
    }

    
    public function addAction(){
        //

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            //Member Detail
            $memberType = $formData['member_type_id'];
            $memberCode = trim($formData['member_code']);
            $memberName = $formData['member_name'];
            $pin        = trim($formData['pin']);
            $sponsorId  = $formData['sponsor_id'];
            $sponsorCode  = $formData['sponsor_code'];
            //$parentId   = $formData['parent_id'];

            //Member Address
            $addr1 = $formData['address_line1'];
            $addr2 = $formData['address_line2'];
            $addrCity = $formData['city'];
            $addrRegion = $formData['region'];
            $addrPostcode  = $formData['postal_code'];
            $addrCountry  = $formData['country'];

            //Member Contact
            $contName = $formData['contact_name'];
            $contMobile = $formData['mobile_no'];
            $contPhone = $formData['phone'];
            $contEmail = $formData['email'];
            $contKtp = $formData['no_ktp'];
            
            //Ahli Waris
            $warisName = $formData['waris_name'];
            $warisMobile = $formData['waris_mobile_no'];
            $warisPhone = $formData['waris_phone'];
            $warisEmail = $formData['waris_email'];
            $warisKtp = $formData['waris_ktp'];

            //Bank Acc
            $accBankName = $formData['bank_name'];
            $accAccountName = $formData['account_name'];
            $accBankNo = $formData['bank_account_no'];
            $accBankCity = $formData['bank_city'];

            $errorMsg = "";


            /*
             * Check SN
             */
            $sn = new Application_Model_Sn();
            if(!$sn->isValidSn($memberCode, $pin)){
                $errorMsg .= "* Kode Member dan PIN tidak cocok atau tidak valid.<br>";
            }

            /*
             * Check HP + KTP + Rekening
             */
            $cekMember = $this->member->isExistMember($contMobile, $contKtp, $accBankNo);
            if ($cekMember["hp"] > 0){
                $errorMsg .= "* Nomor HP sudah terdaftar.<br>";
            }

            if ($cekMember["ktp"] > 0){
                $errorMsg .= "* Nomor KTP sudah terdaftar.<br>";
            }

            if ($cekMember["rek"] > 0){
                $errorMsg .= "* Nomor Rekening sudah terdaftar.<br>";
            }


            if ($errorMsg == ""){

                $dataMember = array(
                        'member_type_id' => $memberType,
                        'member_code' => $memberCode,
                        'member_name' => $memberName,
                        'pin' => $pin,
                        'sponsor_id' => $sponsorId,
                        /*'parent_id' => $parentId,*/
                        'reg_dtm' => new Zend_Db_Expr('NOW()')
                        );

                $dataAddress = array(
                        'addr1' => $addr1,
                        'addr2' => $addr2,
                        'addr3' => $addrCity,
                        'addr4' => $addrRegion,
                        'addr5' => $addrPostcode,
                        'country' => $addrCountry
                        );

                $dataContact = array(
                        'contact_type_id' => 1,
                        'contact_name' => $contName,
                        'mobile_no' => $contMobile,
                        'phone' => $contPhone,
                        'email' => $contEmail,
                        'no_ktp'  => $contKtp
                        );

                $dataAhliWaris = array(
                        'contact_type_id' => 2,
                        'contact_name' => $warisName,
                        'mobile_no' => $warisMobile,
                        'phone' => $warisPhone,
                        'email' => $warisEmail,
                        'no_ktp'  => $warisKtp
                        );

                $dataAccount = array(
                        'account_num' => $memberCode,
                        'bank_name' => $accBankName,
                        'account_name' => $accAccountName,
                        'bank_account_no' => $accBankNo,
                        'bank_city' => $accBankCity
                        );

                $modelMember = new Application_Model_Member();
                $data = $modelMember->addMember($dataMember, $dataAccount, $dataContact, $dataAddress, $dataAhliWaris);

                $code = $data['code'];
                $msg = $data['msg'];
                
                if($code == 0){
                        $func = new Application_Model_Functions();

                        $this->view->code = $code;
                        $this->view->msg = '<legend>Selamat, Member Anda Berhasil ditambahkan: </legend>
                                          <table class="table">
                                            <tbody>
                                                    <tr class="active">
                                                            <td>
                                                                    Nama
                                                            </td>
                                                            <td>
                                                                    '.$memberName.'
                                                            </td>

                                                    </tr>
                                                    <tr>
                                                            <td>
                                                                    Alamat
                                                            </td>
                                                            <td>
                                                                    '.$addr1.' '.$addr2.' '.$addrCity.', '.$addrRegion.' '.$addrPostcode.' - '.$func->getCountry($addrCountry).'
                                                            </td>

                                                    </tr>
                                                    <tr class="active">
                                                            <td>
                                                                    SN
                                                            </td>
                                                            <td>
                                                                    '.$memberCode.'
                                                            </td>

                                                    </tr>
                                            </tbody>
                                    </table>';

                       
                }else{

                    $this->view->code = 1;
                    $this->view->msg = "ERROR ".$msg;

                }

            }else{
                $this->view->code = 1;
                $this->view->msg = "ERROR ".$errorMsg;

                
            }


        }else{
            $modelMember = new Application_Model_Member();
            $memberTypeList = $modelMember->getMemberTypeList();
            //$contactTypeList = $modelMember->getContactTypeList();

            $this->view->sess = (array) $this->sess;
            $this->view->memberTypeList = $memberTypeList;

            $this->view->code = isset($this->param['code']) ? $this->param['code'] : "";
            $this->view->msg = isset($this->param['msg']) ? $this->param['msg'] : "";

            $countryList = new Application_Model_Functions();
            $this->view->countryList = $countryList->countryList();
        }
    }


    public function ahliwarisAction(){
        //

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            $memberId = $this->sess->member_id;

            $warisContactId = $formData['member_contact_id'];
            $warisName = $formData['contact_name'];
            $warisMobile = $formData['mobile_no'];
            $warisPhone = $formData['phone'];
            $warisEmail = $formData['email'];
            $warisKtp = $formData['no_ktp'];

            $dataAhliWaris = array(
                'contact_type_id' => 2,
                'contact_name' => $warisName,
                'mobile_no' => $warisMobile,
                'phone' => $warisPhone,
                'email' => $warisEmail,
                'no_ktp' => $warisKtp
            );

            

            $modelMember = new Application_Model_Member();
            $data = $modelMember->updateAhliWaris($warisContactId, $dataAhliWaris);

            $code = $data['code'];
            $msg = $data['msg'];

            if($code == 0){
                    $this->view->code = $code;
                    $this->view->msg = $msg;
            }else{
                $this->view->code = '1';
                $this->view->msg = "ERROR ".$msg;
            }


        }else{
            $modelMember = new Application_Model_Member();
            $ahliWaris = $modelMember->getAhliWaris($this->sess->member_id);
            
            $this->view->sess = (array) $this->sess;
            $this->view->data = $ahliWaris;

            $this->view->code = isset($this->param['code']) ? $this->param['code'] : "";
            $this->view->msg = isset($this->param['msg']) ? $this->param['msg'] : "";

        }
    }

    public function networkAction()
    {

        $formData  = $this->_request->getPost();
        
        $sBy = (isset($formData['sBy'])) ? $formData['sBy'] : 'member_id';
        $key = (isset($formData['key'])) ? $formData['key'] : $this->sess->member_id;
        $isAjax = (isset($formData['ajax'])) ? $formData['ajax'] : "";
        $level = (isset($formData['level'])) ? $formData['level'] : "";

        if ($isAjax == 1){
            $this->_helper->layout->disableLayout();
        }

        //$dataParent = $this->member->getMember($sBy, $key, "", "");
        $dataParent = $this->member->getMemberDetail($key);

        //jaringan by sponsor_id
        $tmpsp = "";

        //$tmpsp .= "<li class='small'>".$dataParent[0]['member_code'];

        $dataChildSp = $this->member->getSponsorNetwork('member_id', $dataParent['sponsor_id'], $dataParent['member_code']);

        $tmpsp .= $dataChildSp['tree']."\n";

        //jaringan by matrix_id

        $tmp = "";

        if ($dataParent['matrik_id'] > 0){
            $this->view->parent = 1;
            $tmp = "<span id='uplineContent'><a href='javascript: void(0)' onclick='getChild(".$dataParent['matrik_id'].")'>INTRODUCER</a></span>";
        }

        
        $tmp .= "<li title='<center>".strtoupper($dataParent['member_name']."<br>".$dataParent['mobile_no']."<br>".$dataParent['addr3']." ".$dataParent['addr4'])."</center>' class='small'><br>".str_replace('CBR','CBR<br>',str_replace('NIK','NIK<br>',$dataParent['member_code']));
        
        $dataChild = $this->member->getMemberNetwork('matrik_id', $key, $dataParent['member_code'], $level);

        $tmp .= $dataChild['tree']."
                </li>\n";


        $this->view->sponsorId =  $dataParent['sponsor_id'];
        $this->view->sponsor = $tmpsp;
        $this->view->matrix = $tmp;
        $this->view->isAjax = $isAjax;
        
    }


    public function treenaryAction()
    {

        $formData  = $this->_request->getPost();

        $sBy = (isset($formData['sBy'])) ? $formData['sBy'] : 'member_id';
        $key = (isset($formData['key'])) ? $formData['key'] : $this->sess->member_id;
        $isAjax = (isset($formData['ajax'])) ? $formData['ajax'] : "";

        if ($isAjax == 1){
            $this->_helper->layout->disableLayout();
        }

        $dataParent = $this->member->getMemberDetail($key);


        
        //jaringan by treenary_id

        $tmp = "";

        if ($dataParent['matrik_id'] > 0){
            $this->view->parent = 1;
            $tmp = "<span id='uplineContent'><a href='javascript: void(0)' onclick='getTreeChild(".$dataParent['treenary_id'].")'>INTRODUCER</a></span>";
        }

        $tmp .= "<li title='<center>".strtoupper($dataParent['member_name']."<br>".$dataParent['mobile_no']."<br>".$dataParent['addr3']." ".$dataParent['addr4'])."</center>' class='small'><br>".str_replace('CBR','CBR<br>',str_replace('NIK','NIK<br>',$dataParent['member_code']));

        $dataChild = $this->member->getMemberTreenary('treenary_id', $key, $dataParent['member_code']);

        $tmp .= $dataChild['tree']."
                </li>\n";


        $this->view->sponsorId =  $dataParent['sponsor_id'];
        $this->view->matrix = $tmp;
        $this->view->isAjax = $isAjax;

    }

    public function infonetworkAction(){

        //

    }

    public function rewardAction()
    {
        //$auth = Zend_Auth::getInstance();
        //$identity = $auth->getIdentity();
        $memberId = $this->sess->member_id;

        
        $rewardList = $this->member->getMemberDetail($memberId);

        

        $this->view->rewardList = $rewardList;

    }
}

